import { defineComponent, reactive, ref, computed } from "vue";
import Page from "@/components/public/page/page";
import { CardWithTitle } from "@/components/public/card/card";
import {
  SearchOutlined,
} from "@ant-design/icons-vue";
import LTable from "@/components/public/lTable/lTable";
import secondaryChannelsService from "@/modules/channelManagement/secondaryChannels.service";
import secondaryChannelsPageAuth from "./personalSecondaryPage.auth";
import useNavigator from "@/hooks/useNavigator";
import SearchQRcode from "@/views/index/channelManagement/promotionChannels/secondaryPage/popup/searchCode/searchQRcode";
export default defineComponent({
  setup() {
    const navigator = useNavigator();
    const qrcodeRef = ref(null as any);
    const state = reactive({
      toolbar: {
        left: ["refresh","output"],
        right: ["filter", "search"],
      },
      dataSource: [],
      columns: [
        {
          title: "ID",
          dataIndex: "id",
          align: "center",
          width: "100px",
        },
        {
          title: "用户名",
          dataIndex: "username",
          align: "center",
          width: "100px",
        },
        {
          title: "类型",
          dataIndex: "type",
          align: "center",
          width: "100px",
        },
        {
          title: "渠道名称",
          dataIndex: "name",
          align: "center",
          width: "180px",
        },
        {
          title: "负责人姓名",
          dataIndex: "realname",
          align: "center",
          width: "100px",
        },
        {
          title: "联系电话",
          dataIndex: "phone",
          align: "center",
          width: "100px",
        },
        {
          title: "创建时间",
          dataIndex: "create_start_time",
          align: "center",
          width: "150px",
        },
        {
          fixed: "right",
          title: "操作",
          dataIndex: "operation",
          align: "center",
          width: "100px",
        },
      ],
      loading: false,
      query: {
        pid: navigator.route?.query?.pid || '',
      },
      searchForm: {
        insurer_name: {
          type: "input",
          label: "被保人姓名",
          attrs: { placeholder: "被保人姓名" },
        },
        insurer_idno: {
          type: "input",
          label: "被保人身份证号",
          attrs: { placeholder: "被保人身份证号" },
        },
        create_time: {
          type: "date_range",
          label: "创建日期",
        },
        status: {
          type: "select",
          label: "状态",
          attrs: { placeholder: "状态" },
          datas: [
            { label: "已拒绝", value: 10 },
            { label: "处理中", value: 20 },
            { label: "已通过", value: 30 },
          ],
        },
      },
      pagination: {
        total: 0,
        current: 1,
        pageSize: 10,
      },
    } as any);
    // 表格状态变更
    const tableChangeHandler = (pag: { pageSize: number; current: number }) => {
      state.pagination.current = pag.current;
      state.pagination.pageSize = pag.pageSize;
    };
    // 选中项变更
    const onSelectChange = (changableRowKeys: any[]) => {
      state.selectedRowKeys = changableRowKeys;
    };
    const rowSelection = computed(() => {
      return {
        selectedRowKeys: state.selectedRowKeys,
        fixed: true,
        onChange: onSelectChange,
      };
    });
    // 服务
    const $service = new secondaryChannelsService();
    // 表格节点
    const LTableRef = ref(null as any);
    const DetailRef = ref(null as any);
    return () => (
      <Page>
        <CardWithTitle title="二级渠道">
            <LTable
              ref={LTableRef}
              service={$service}
              toolbar={state.toolbar}
              v-model={state}
            >
              <a-table
                bordered={true}
                scroll={{ x: "max-content" }}
                dataSource={state.dataSource}
                columns={state.columns}
                pagination={state.pagination}
                onChange={tableChangeHandler}
                row-selection={rowSelection.value}
                loading={state.loading}
                rowKey="id"
                v-slots={{
                  bodyCell: ({ column, record }: any) => {
                    // // 操作栏
                    if (column.dataIndex === "operation") {
                      return (
                        <div class="operation">
                            <a-button
                              type="primary"
                              size="small"
                              v-slots={{
                                icon: () => <SearchOutlined />,
                              }}
                              onClick={() => {
                                navigator.push({ name: 'personalThreeLevelChannelPage', query: { pid: record.id} })
                              }}
                            >
                              查看三级渠道
                            </a-button>
                            <a-button
                              type="primary"
                              size="small"
                              v-slots={{
                                icon: () => <SearchOutlined />,
                              }}
                              onClick={() => {
                                console.log(process.env.VUE_APP_BASE_API);
                                qrcodeRef.value.open({
                                  id:record.id,
                                  url:(process.env.VUE_APP_BASE_API == "https://biztest.zljagroup.com")?(
                                    `https://biz.zljagroup.com/webtest/women-health?id={id}/#/saler`
                                  ):(`https://biz.zljagroup.com/web/women-health?id={id}/#/saler`)
                               })
                              }}
                            >
                              查看二维码
                            </a-button>
            
                        </div>
                      );
                    }
                    if (column.dataIndex === "type") {
                      return record.type ? (
                        <div>出单机构</div>
                      ) : (
                        <div>推广机构</div>
                      );
                    }
                  },
                }}
              />
            </LTable>
        </CardWithTitle>
        <SearchQRcode 
          ref={qrcodeRef}
        />
      </Page>
    );
  },
});